package com.mocaris.android.gank.vm

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.mocaris.android.gank.data.LiveDataResouce
import com.mocaris.android.gank.data.WanAndroidRepository
import com.mocaris.android.gank.model.Article
import com.mocaris.android.gank.model.Banner
import com.mocaris.android.gank.model.SystemTree

class AndroidMainViewModel(application: Application) : BaseViewModel(application) {

    private val wanAndroidRepository: WanAndroidRepository by lazy {
        WanAndroidRepository()
    }

    val bannerData: MutableLiveData<LiveDataResouce<List<Banner>>> =
        MutableLiveData<LiveDataResouce<List<Banner>>>()

    val systemTree: MutableLiveData<LiveDataResouce<List<SystemTree>>> = MutableLiveData()

    /**
     * banner
     */
    @SuppressLint("CheckResult")
    fun getBanner() {
        wanAndroidRepository.getBanner().subscribe({
            bannerData.postValue(it)
        }, {
            bannerData.postValue(LiveDataResouce(-1, "获取失败"))
        })
    }


    /**
     * 首页文章列表
     */
    @SuppressLint("CheckResult")
    fun getHomeArticle(page: Int): MutableLiveData<LiveDataResouce<Pair<List<Article>, Int>>> {
        val homeArticle = MutableLiveData<LiveDataResouce<Pair<List<Article>, Int>>>()
        wanAndroidRepository.getHomeArticle(page).subscribe({
            homeArticle.postValue(it)
        }, {
            homeArticle.postValue(LiveDataResouce(-1, "获取失败"))
        })
        return homeArticle
    }

    /**
     * 知识体系
     */
    fun getSysTree() {
        wanAndroidRepository.getSysTree()
            .subscribe({
                systemTree.postValue(LiveDataResouce(it))
            }, {
                systemTree.postValue(LiveDataResouce(-1, it.message ?: ""))
            })
    }
}
